package com.yunhe.transfer.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.yunhe.common.model.base.DataTransferDTO;
import com.yunhe.common.model.data.Measurement;
import com.yunhe.common.util.DateTimeUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @title: MeasurementUtil
 * @Author Chen Lun
 * @Date: 2021/1/25
 */
public class MeasurementUtil {
    /**
     * 接受信息转换历史量测
     * @param mapList
     * @param rtime
     * @return
     */
    public static List<Measurement> map2Meas(List<Map<String, Object>> mapList, Optional<Object> rtime) {
        List<Measurement> measurements = new ArrayList<>(mapList.size());
        Long rtimeGet = null;
        if (rtime.isPresent()) {
            rtimeGet = ((Number)rtime.get()).longValue();
        }
        for (Map<String, Object> map : mapList) {
            Measurement measurement = new Measurement();
            measurement.setPointNumber(Long.parseLong(map.get("pointNumber").toString()));
            measurement.setVal(((Number)map.get("val")).doubleValue());
            Object measurementRtime = map.get("rtime");
            if (rtime != null) {
                rtimeGet = ((Number)measurementRtime).longValue();
            }
            measurement.setRtime(rtimeGet);
            measurement.setDtime(DateTimeUtils.getDateString(rtimeGet, null));
            measurements.add(measurement);
        }
        return measurements;
    }

    /**
     * 获取相对应的数据转移对象
     * 目前主要为量测历史，项目代号和起止时间戳
     * @param measurements
     * @param stationCode
     * @return
     */
    public static DataTransferDTO recollectData(List<Measurement> measurements, Long stationCode) {
        DataTransferDTO dataTransferDTO = new DataTransferDTO();
        dataTransferDTO.setMeasurements(measurements);
        dataTransferDTO.setStationCode(stationCode);
//        Set<Long> collect = measurements.stream()
//                .map(Measurement::getRtime)
//                .sorted(Long::compareTo)
//                .collect(Collectors.toCollection(TreeSet::new));
        Optional<Long> max = measurements.stream().map(Measurement::getRtime).max(Long::compareTo);
        Optional<Long> min = measurements.stream().map(Measurement::getRtime).min(Long::compareTo);
        dataTransferDTO.setStartRtime(min.orElse(0L));
        dataTransferDTO.setEndRtime(max.orElse(0L));
        return dataTransferDTO;
    }

    /**
     * 处理量测列表rtime
     * @param measurements
     * @param timezone
     * @return
     */
    public static List<Measurement> processMeasurements(List<Measurement> measurements, String timezone) {
        for (Measurement item : measurements) {
            // 如果没有上传数据时间rtime，则需要处理rtime
            if (item.getRtime() == null) {
                long rtime = 0L;
                if (item.getDtime() != null) {
                    rtime = DateTimeUtils.getTimestamp(item.getDtime(), timezone);
                } else {
                    rtime = System.currentTimeMillis();
                }
                item.setRtime(rtime);
            }
        }
        return measurements;
    }
}
